#include "WProgram.h"
#include "Player.h"

void Player::begin(Sound & sound, Tagger & tagger) {
  //see the declration of the variables in player.h for some explanation on them
  team = 7;
  number = 0;
  isAlive = true;
  wakeUpMoment = 0;
  anyKindOfRespawn = true;
  hasDeathSound = true;
  respawnDelay = 10; //in seconds set to 0 for NO respawn
  alliedRespawn = false;
  baseRespawn = false;
  friendlyFire = true;
  alliedHeal = false;
  baseHeal = false;
  startingLifePoints = 100;
  lifePoints = 100;
  maxLifePoints = 100;
  hitResponseTime = 60;
  reloadMoment = 0;

  Player::sound = &sound;

  //thre is no team or player n selection by the side of the player
  team = 1;
  number = 1;

  //MAB we have to be sure that what we have now inside equipedTagger is
  //the memory adress of the object tagger not
  //the memory adress of the memory adress of tagger
  equipedTagger = &tagger;
  Player::nClips = equipedTagger->nClips;

  sound.playerReady();
}

/*
GENERAL CONSIDERATIONS ABOUT HEALTH FUNCTIONS:
 when changing life points always remember that lifePoints is a uint8_t so we should never overflow or underflow it
 all lifepoints info is stored in uint8_ts so there are no negative values, thats why we use two different functions
 for life add and substract
 */
void Player::hurt(uint8_t attackingDamage, uint8_t attackingPlayer, uint8_t attackingTeam){
  //IMPROVEMENT store in memory data about the player that has hit the player (in that case we'll need info for that)
  if(lifePoints <= attackingDamage){
    lifePoints = 0; //the player is dead

    sound->death();

    if(anyKindOfRespawn){

    }
    else{
      sound->gameOver();
    }
    isAlive = false;

    //IMPROVEMENT
    //if the player is dead we want to store who killed him
    //EEPROM.write(lastWrittenBlock, (attackingTeam << 5) + attackingPlayer); 
    //the seccond parameter is the uint8_t holding the team and player info in the following format TTTP PPPP
    //lastWrittenBlock ++;
  }
  else {
    lifePoints -= attackingDamage;
    sound->hit();
    wakeUpMoment =  millis() + hitResponseTime*10; //the time the player spends inactive is equal to hitResponseTime*10 miliseconds
  }
}

void Player::heal(uint8_t lifePoints){
  if(lifePoints >= (maxLifePoints - lifePoints) ){
    lifePoints = maxLifePoints;
  }
  else{
    lifePoints += lifePoints;
  }
}

void Player::startRespawn(){
  sound->respawnStart();
  respawnMoment = millis()+(respawnDelay*1000);
}

void Player::endRespawn(){
  //once the respawn is completed
  lifePoints = startingLifePoints;
  nClips = equipedTagger->startingClips;
  isAlive = true;
  wakeUpMoment = 0;
  sound->playerReady();
}


void Player::refreshStatus(){
  if((reloadMoment != 0) && (millis() > reloadMoment)){
    endReload();
  }

  if((respawnMoment != 0) && (millis() > respawnMoment)){
    endRespawn();
  }

  //0 is the default value of wakeUpMoment. 0 means that the player is awake
  if(isAlive){
    if(wakeUpMoment != 0 && wakeUpMoment < millis()){ 
      wakeUpMoment = 0;
    }
  }
  else {
    if(respawnDelay>0){
      //autorespawn
      startRespawn();
    }
  }
}

/*
 * reloads the tagger carried by the player if possible
 * if the player does not have clips the tagger does not reload and it returns false
 * playing a sad sound would be nice
 */

void Player::startReload(){
  if(nClips==0){
    sound->outOfAmmo();
  }
  else {
    reloadMoment = millis()+(equipedTagger->reloadDelay*1000);
    sound->startedReloading();
  }
}

//void Player::refreshReloadStatus(){
  //TODO WAIT FOR THE RELOAD TO COMPLETE

  //do something like led blink without delay to indicate the status
  /*
	if (millis() - previousMillis > interval) {
   	// save the last time you blinked the LED 
   	previousMillis = millis();   
   	
   	// if the LED is off turn it on and vice-versa:
   	if (ledState == LOW)
   	ledState = HIGH;
   	else
   	ledState = LOW;
   	
   	// set the LED with the ledState of the variable:
   	digitalWrite(ledPin, ledState);
   	*/
//}

void Player::endReload(){
  reloadMoment = 0;
  //once the time has passed decrease the clips
  nClips--;

  //and increase the ammo
  equipedTagger->nAmmo = equipedTagger->clipSize;

  //play the reload finished melody
  sound->finishedReloading();
}


